package server.content.skilling;

import server.Server;
import server.event.Event;
import server.event.EventContainer;
import server.event.EventManager;
import server.game.players.Client;

/**
 * 
 * @author Miracle
 * 
 */
public abstract class PlayerSkill {

	private final Client c;
	private final int SKILL_ID;
	public int coolDown = -1;
	private boolean isActive;
	private int tick = -1;
	public boolean antiBot = false;
	public long startTime = -1;

	public PlayerSkill(final Client c, final int skillId) {
		this.c = c;
		this.SKILL_ID = skillId;
	}

	public abstract void init();

	public abstract void process();

	public abstract void kill();

	public void deleteItem(final int id, final int amnt) {
		c.getItems().deleteItem(id, amnt);
	}

	public void doAnimation(final int anim) {
		c.startAnimation(anim);
		c.getPA().requestUpdates();
	}

	public void addItem(final int id, final int amnt) {
		c.getItems().addItem(id, amnt);
	}

	public void addXP(final int xp, final int skill) {
		c.getPA().addSkillXP(xp, skill);
	}

	public boolean playerHasItem(final int itemId) {
		return c.getItems().playerHasItem(itemId);
	}

	public boolean playerHasItem(final int itemId, final int amnt) {
		return c.getItems().playerHasItem(itemId, 1);
	}

	public void resetAnim() {
		c.startAnimation(65535);
		c.getPA().requestUpdates();
	}

	public int getTick() {
		return tick;
	}

	public void setTick(final int TICK) {
		this.tick = TICK;
	}

	public void setActive(final boolean isActive) {
		this.isActive = isActive;
	}

	public boolean isActive() {
		return isActive;
	}

	public int getSkillId() {
		return SKILL_ID;
	}

	public int getPlayerLevel() {
		return c.playerLevel[getSkillId()];
	}

	public boolean invIsFull() {
		return c.getItems().freeSlots() < 1;
	}

	public void sendMessage(final String s) {
		c.sendMessage(s);
	}
	
	public void coolDown() {
		EventManager.getSingleton().addEvent(new Event() {

			@Override
			public void execute(EventContainer container) {
				if (coolDown > 0) {
					coolDown--;
				} else if (coolDown == 0) {
					coolDown = -1;
					antiBot = false;
					startTime = -1;
					container.stop();
				}

			}
		}, 2);
	}

	public Client getClient() {
		return c;
	}

	public void enableBotProtection() {
		antiBot = true;
		coolDown = 250;
		c.sendMessage("@red@[BOT PROTECTION] - This skill has received a cooldown timer.");
		coolDown();
		doAnimation(65535);
		setActive(false);
		// Server.npcHandler.spawnNpc(c, 599, c.getX(), c.getY(), 0, 0, 100, 5,
		// 5,
		// 4, false, false);
	}

	public boolean antiBotEnabled() {
		return antiBot;
	}

	public static boolean isSkillClass(final Class<?> clazz) {
		Object clazs = null;
		try {
			clazs = clazz.newInstance();
		} catch (final Exception e) {
			e.printStackTrace();
		}
		return (clazs instanceof PlayerSkill);
	}

}
